#include "drv_usart2.h"
#include "timer0.h"
#include <stdarg.h>
#include <stdio.h>

#define USART2_MAX_RECV_LEN		512



static u8 *pRecvBuf = NULL;
static u16 recvLength = 0;
static u16 recvIndex = 0;
static u32 baudRate = 115200;

/******************************************************************************/
/**
 *******************************************************************************
 ** \brief USART RX irq callback function.
 **
 ** \param [in] None
 **
 ** \retval None
 **
 ******************************************************************************/
static void UsartRxIrqCallback(void){
	uint16_t u16Data = USART_RecData(M4_USART2);
	if(pRecvBuf != NULL){
		pRecvBuf[recvIndex%USART2_MAX_RECV_LEN] = (u8)u16Data;
	}
	recvIndex ++;
	USART_ClearStatus(M4_USART2, UsartRxNoEmpty);
	USART_FuncCmd(M4_USART2, UsartTxAndTxEmptyInt, Enable);
}

/**
 *******************************************************************************
 ** \brief USART RX error irq callback function.
 **
 ** \param [in] None
 **
 ** \retval None
 **
 ******************************************************************************/
static void UsartErrIrqCallback(void){
    if (Set == USART_GetStatus(M4_USART2, UsartFrameErr)){
        USART_ClearStatus(M4_USART2, UsartFrameErr);
		//printf("UsartFrameErr\n");
    }else{

    }
    if (Set == USART_GetStatus(M4_USART2, UsartParityErr)){
        USART_ClearStatus(M4_USART2, UsartParityErr);
		//printf("UsartParityErr\n");
    }else{

    }

    if (Set == USART_GetStatus(M4_USART2, UsartOverrunErr)){
        USART_ClearStatus(M4_USART2, UsartOverrunErr);
		//printf("UsartOverrunErr\n");
    }else{

    }
}

void USART2_Init(u32 baud){
	baudRate = baud;
	/* 初始化信号量 */
	uint16_t u16RxData;
	stc_irq_regi_conf_t stcIrqRegiCfg;

	en_result_t enRet = Ok;

	const stc_usart_uart_init_t stcInitCfg = {
		UsartIntClkCkNoOutput,
		UsartClkDiv_4,
		UsartDataBits8,
		UsartDataLsbFirst,
		UsartOneStopBit,
		UsartParityNone,
		UsartSampleBit8,
		UsartStartBitFallEdge,
		UsartRtsEnable,
	};

	/* Enable peripheral clock */
	PWC_Fcg1PeriphClockCmd(PWC_FCG1_PERIPH_USART2, Enable);


	/* Initialize USART IO */
	PORT_SetFunc(USART2_RX_PORT, USART2_RX_PIN, USART2_RX_FUNC, Disable);
	PORT_SetFunc(USART2_TX_PORT, USART2_TX_PIN, USART2_TX_FUNC, Disable);

	/* Initialize UART */
	enRet = USART_UART_Init(M4_USART2, &stcInitCfg);
	if (enRet != Ok){
		while (1){
			printf("USART_UART_Init error\n");
		}
	}
	/* Set baudrate */
	enRet = USART_SetBaudrate(M4_USART2, baud);
	if (enRet != Ok){
		while (1){
			printf("USART_SetBaudrate error\n");
		}
	}
	/* Set USART RX IRQ */
	stcIrqRegiCfg.enIRQn = Int006_IRQn;
	stcIrqRegiCfg.pfnCallback = &UsartRxIrqCallback;
	stcIrqRegiCfg.enIntSrc = INT_USART2_RI;
	enIrqRegistration(&stcIrqRegiCfg);
	NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
	NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
	NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);

	/* Set USART RX error IRQ */
	stcIrqRegiCfg.enIRQn = Int007_IRQn;
	stcIrqRegiCfg.pfnCallback = &UsartErrIrqCallback;
	stcIrqRegiCfg.enIntSrc = INT_USART2_EI;
	enIrqRegistration(&stcIrqRegiCfg);
	NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
	NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
	NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);

	USART_FuncCmd(M4_USART2, UsartTimeOut, Enable);
	USART_FuncCmd(M4_USART2, UsartTimeOutInt, Enable);

	/*Enable RX && TX function*/
	USART_FuncCmd(M4_USART2, UsartRx, Enable);
	USART_FuncCmd(M4_USART2, UsartTx, Enable);
	USART_FuncCmd(M4_USART2, UsartRxInt, Enable);

	/*Enable RX && TX function*/
	USART_FuncCmd(M4_USART2, UsartRx, Enable);	
	USART_FuncCmd(M4_USART2, UsartRxInt, Enable);
	USART_FuncCmd(M4_USART2, UsartTimeOut, Enable);
	USART_FuncCmd(M4_USART2, UsartTimeOutInt, Enable);
	USART_FuncCmd(M4_USART2, UsartTx, Enable);
}



void Iot_write(u8 *buffer, u16  size){
	u16 i=0;
	for(i=0;i<size;i++){
		while (Reset == USART_GetStatus(M4_USART2, UsartTxEmpty));
		USART_SendData(M4_USART2,buffer[i]);
	}
}






